package com.mk.legoscans2.data;

import com.mk.legoscans2.LegoScans3Application;
import com.mk.legoscans2.helpers.AccountAlertDialogHelper;

public class AccountService {
	
	ApplicationPreferences _applicationPreferences;
	
	public AccountService(ApplicationPreferences applicationPreferences) {
		_applicationPreferences = applicationPreferences;
		
		initAccount();
	}
	
	private void initAccount() {
		String account = _applicationPreferences.getAccount();
		if(account == ""){
			_applicationPreferences.setAccount(getDefaultAccountName().toString());
		}
	}

	public AccountType getAccoutType() {
		String account = _applicationPreferences.getAccount().toLowerCase();
		
		if(account.contains("@"))
			return AccountType.google;
		
		return AccountType.local;
	}
	
	public String getAccount() {
		return _applicationPreferences.getAccount();
	}
	
	public CharSequence getDefaultAccountName(){
		CharSequence[] getAccountNames = AccountAlertDialogHelper.getAccountNames(LegoScans3Application.getContext());
		
		CharSequence defaultAccount = "Local";
		for(CharSequence s : getAccountNames){
			if(s.toString().contains("@")){
				defaultAccount = s;
				break;
			}
		}
		
		return defaultAccount;
	}
}
